unit uPesquisa_Locacao;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uPai, DB, StdCtrls, DBCtrls, Buttons, ExtCtrls, ComCtrls, Grids,
  DBGrids, Provider, SqlExpr, DBClient, DBLocal, DBLocalS, ImgList, dxCore,
  dxButton, XPMenu, FMTBcd, rpcompobase, rpvclreport, RXSpin, JvExDBGrids,
  JvDBGrid;

type
  TfPesquisa_Locacao = class(TfPai)
    DBMemo1: TDBMemo;
    ImageList1: TImageList;
    GroupBox1: TGroupBox;
    CB2: TComboBox;
    Label3: TLabel;
    GroupBox3: TGroupBox;
    BitBtn3: TdxButton;
    BitBtn4: TdxButton;
    BitBtn5: TdxButton;
    dxButton1: TdxButton;
    BitBtn6: TdxButton;
    XPMenu1: TXPMenu;
    scdsCp_proc: TSQLDataSet;
    DataSetProvider1: TDataSetProvider;
    cds: TClientDataSet;
    VCLReport1: TVCLReport;
    Panel1: TPanel;
    Label1: TLabel;
    Label4: TLabel;
    q1: TRxSpinEdit;
    q2: TRxSpinEdit;
    Label9: TLabel;
    Label10: TLabel;
    Label7: TLabel;
    Label12: TLabel;
    s1: TRxSpinEdit;
    c1: TRxSpinEdit;
    g1: TRxSpinEdit;
    s2: TRxSpinEdit;
    c2: TRxSpinEdit;
    g2: TRxSpinEdit;
    cbimovel: TComboBox;
    Label6: TLabel;
    Label2: TLabel;
    cbTipo: TComboBox;
    cbRua: TComboBox;
    Label5: TLabel;
    valorDe: TEdit;
    Label8: TLabel;
    valorAte: TEdit;
    Label13: TLabel;
    cbBairro: TComboBox;
    Label11: TLabel;
    cbCidade: TComboBox;
    Label14: TLabel;
    DBGrid1: TJvDBGrid;
    OpenDialog1: TOpenDialog;
    BitBtn7: TdxButton;
    Panel4: TPanel;
    Image1: TImage;
    BitBtn1: TBitBtn;
    dxButton2: TdxButton;
    scdsCp_procCOD_IMALU: TIntegerField;
    scdsCp_procCOD_IMOBILIARIA: TStringField;
    scdsCp_procIMOVEL: TStringField;
    scdsCp_procTIPO: TStringField;
    scdsCp_procNR: TStringField;
    scdsCp_procCOMPL: TStringField;
    scdsCp_procVALOR: TFloatField;
    scdsCp_procOBS: TStringField;
    scdsCp_procATIVO: TStringField;
    scdsCp_procALUGADO: TStringField;
    scdsCp_procVISITA: TStringField;
    scdsCp_procCOD_INTERESSADO: TIntegerField;
    scdsCp_procRUA: TStringField;
    scdsCp_procBAIRRO: TStringField;
    scdsCp_procCIDADE: TStringField;
    scdsCp_procCEP: TStringField;
    scdsCp_procUF: TStringField;
    cdsCOD_IMALU: TIntegerField;
    cdsCOD_IMOBILIARIA: TStringField;
    cdsIMOVEL: TStringField;
    cdsTIPO: TStringField;
    cdsNR: TStringField;
    cdsCOMPL: TStringField;
    cdsVALOR: TFloatField;
    cdsOBS: TStringField;
    cdsATIVO: TStringField;
    cdsALUGADO: TStringField;
    cdsVISITA: TStringField;
    cdsCOD_INTERESSADO: TIntegerField;
    cdsRUA: TStringField;
    cdsBAIRRO: TStringField;
    cdsCIDADE: TStringField;
    cdsCEP: TStringField;
    cdsUF: TStringField;
    Label15: TLabel;
    cbProprietario: TComboBox;
    procedure BitBtn3Click(Sender: TObject);
    procedure BitBtn4Click(Sender: TObject);
    procedure BitBtn6Click(Sender: TObject);
    procedure BitBtn7Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure DBGrid1TitleClick(Column: TColumn);
    procedure DBGrid1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure DBGrid1KeyUp(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure DBGrid1DblClick(Sender: TObject);
    procedure DBGrid1CellClick(Column: TColumn);
    procedure DBGrid1ColEnter(Sender: TObject);
    procedure DBGrid1ColExit(Sender: TObject);
    procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    procedure dxButton1Click(Sender: TObject);
    procedure dxButton2Click(Sender: TObject);
    procedure BitBtn5Click(Sender: TObject);
  private
    { Private declarations }
    procedure ChkDBGridDrawColumnCell(DBGrid: TDBGrid;
      const Rect: TRect; DataCol: Integer; Column: TColumn;
      State: TGridDrawState);
    procedure ChkDBGridColEnter(DBGrid: TDBGrid);
    procedure ChkDBGridColExit(DBGrid: TDBGrid);
    procedure ChkDBGridCellClick(Column: TColumn);
    procedure ChkDBGridKeyPress(DBGrid: TDBGrid; var Key: Char);
  public
    { Public declarations }
  end;

var
  fPesquisa_Locacao: TfPesquisa_Locacao;

implementation

uses UDM, uFoto, uFotoVenda, uImovel_aluguel, uImovel_venda, uProcurar,
  uRel_Pesq_locacao, uVisitar, uMostraVisita, uAlugar_Imovel, sCtrlResize;

{$R *.dfm}

procedure TfPesquisa_Locacao.BitBtn3Click(Sender: TObject);
var
    sqltexto, sqltexto1 : string;
begin

   if (cds.Active) then
       cds.Close;
   cds.CommandText := '';
   sqltexto := '';
   sqltexto1 := '';
   sqltexto := 'select ima.COD_IMALU, ima.COD_IMOBILIARIA, ima.IMOVEL, ima.TIPO, ' +
          'ima.NR, ima.COMPL, ima.VALOR_ALUGUEL as VALOR, ima.OBS, ima.ATIVO, ' +
          'ima.ALUGADO, ima.VISITA, ima.COD_INTERESSADO, cep.RUA, cep.BAIRRO, ' +
          'cep.CIDADE, cep.CEP, cep.UF, pro.NOME_PROPRIETARIO from IMOVEL_ALUGUEL ima ' +
          'left outer join PROPRIETARIOS pro on pro.COD_PROPRIETARIO = ima.COD_PROPRIETARIO ' +
          'left outer join CEP cep on cep.COD_CEP=ima.COD_ENDERECO ';

   if (cbRua.Text <> '') then
     sqltexto1 := ' where UDF_COLLATEBR(cep.RUA) containing UDF_COLLATEBR(' + QuotedStr(cbRua.Text) + ')';
  //------------------------------------------------------------------------------
  //PESQUISA PROPRIETARIO
  //------------------------------------------------------------------------------
  if cbProprietario.Text <> '' then
  begin
    if sqlTexto1='' then
      sqlTexto1 := sqlTexto1 + ' where UDF_COLLATEBR(pro.NOME_PROPRIETARIO) containing UDF_COLLATEBR(' + QuotedStr(cbProprietario.Text) + ')'
    else
      sqlTexto1 := sqlTexto1 + ' and UDF_COLLATEBR(pro.NOME_PROPRIETARIO) containing UDF_COLLATEBR(' + QuotedStr(cbProprietario.Text) + ')';
  end;

   if (cbBairro.Text <> '') then
   if (sqltexto1 = '') then
     sqltexto1 := ' where UDF_COLLATEBR(ima.BAIRRO) containing UDF_COLLATEBR(' + QuotedStr(cbBairro.Text) + ')'
   else
     sqltexto1 := sqltexto1 + ' and UDF_COLLATEBR(ima.BAIRRO) containing UDF_COLLATEBR(' + QuotedStr(cbBairro.Text)+  ')';

   if (cbCidade.Text <> '') then
   if (sqltexto1 = '') then
     sqltexto1 := ' where UDF_COLLATEBR(ima.CIDADE) containing UDF_COLLATEBR(' + QuotedStr(cbCidade.Text) + ')'
   else
     sqltexto1 := sqltexto1 + ' and UDF_COLLATEBR(ima.CIDADE) containing UDF_COLLATEBR(' + QuotedStr(cbCidade.Text) + ')';

   if ((valorDe.Text <> '') and (valorAte.Text <> '')) then
      if (sqltexto1 = '') then
        sqltexto1 := ' where ima.VALOR_ALUGUEL between ' + (valorDe.Text) + ' and ' + (valorAte.Text)
      else
        sqltexto1 := sqltexto1 + ' and ima.VALOR_ALUGUEL between ' + (valorDe.Text) + ' and ' + (valorAte.Text);

   if (CB2.Text = 'PARA ALUGAR') then
   if (sqltexto1 = '') then
     sqltexto1 := ' where UDF_COLLATEBR(ima.ALUGADO) containing ' + QuotedStr('N')
   else
     sqltexto1 := sqltexto1 + ' and UDF_COLLATEBR(ima.ALUGADO) containing ' + QuotedStr('N');

   if (CB2.Text = 'ALUGADO') then
   if (sqltexto1 = '') then
     sqltexto1 := ' where UDF_COLLATEBR(ima.ALUGADO) containing ' + QuotedStr('S')
   else
     sqltexto1 := sqltexto1 + ' and UDF_COLLATEBR(ima.ALUGADO) containing ' + QuotedStr('S');

   if (cbimovel.Text <> '') then
   if (sqltexto1 = '') then
     sqltexto1 := ' where ima.IMOVEL = ' + QuotedStr(cbimovel.Text)
   else
     sqltexto1 := sqltexto1 + ' and ima.IMOVEL = ' + QuotedStr(cbimovel.Text);

   if (cbTipo.Text <> '') then
   if (sqltexto1 = '') then
     sqltexto1 := ' where ima.TIPO = ' + QuotedStr(cbTipo.Text)
   else
     sqltexto1 := sqltexto1 + ' and ima.TIPO = ' + QuotedStr(cbTipo.Text);

   if ((q1.Text <> '0') and (q2.Text <> '0')) then
      if (sqltexto1 = '') then
        sqltexto1 := ' where ima.N_QUARTOS between ' + QuotedStr(q1.Text) + ' and ' + QuotedStr(q2.Text)
      else
        sqltexto1 := sqltexto1 + ' and ima.N_QUARTOS between ' + QuotedStr(q1.Text) + ' and ' + QuotedStr(q2.Text);

   if ((s1.Text <> '0') and (s2.Text <> '0')) then
      if (sqltexto1 = '') then
        sqltexto1 := ' where ima.N_SALAS between ' + QuotedStr(s1.Text) + ' and ' + QuotedStr(s2.Text)
      else
        sqltexto1 := sqltexto1 + ' and ima.N_SALAS between ' + QuotedStr(s1.Text) + ' and ' + QuotedStr(s2.Text);

   if ((c1.Text <> '0') and (c2.Text <> '0')) then
      if (sqltexto1 = '') then
        sqltexto1 := ' where ima.N_COZINHAS between ' + QuotedStr(c1.Text) + ' and ' + QuotedStr(c2.Text)
      else
        sqltexto1 := sqltexto1 + ' and ima.N_COZINHAS between ' + QuotedStr(c1.Text) + ' and ' + QuotedStr(c2.Text);

   if ((g1.Text <> '0') and (g2.Text <> '0')) then
      if (sqltexto1 = '') then
        sqltexto1 := ' where ima.N_GARAGENS between ' + QuotedStr(g1.Text) + ' and ' + QuotedStr(g2.Text)
      else
        sqltexto1 := sqltexto1 + ' and ima.N_GARAGENS between ' + QuotedStr(g1.Text) + ' and ' + QuotedStr(g2.Text);

   if (sqltexto1 = '') then
       sqltexto1 := ' where ima.VALOR > 0'
      else
        sqltexto1 := sqltexto1 + ' and ima.VALOR_ALUGUEL > 0 ';


   sqltexto := sqltexto + sqltexto1;

   cds.CommandText := sqltexto;
   cds.Open;
   cds.First;

   // mostro a primeira foto
   if dm.cds_foto.Active then
      dm.cds_foto.Close;
   dm.cds_foto.Params[0].Clear;
   dm.cds_foto.Params[0].AsString := IntTostr(cdsCOD_IMALU.AsInteger) + '-L';
   dm.cds_foto.Open;
   if not dm.cds_foto.IsEmpty then
     Image1.Picture.LoadFromFile(dm.cds_fotoNOME_FOTO.AsString);

end;

procedure TfPesquisa_Locacao.BitBtn4Click(Sender: TObject);
begin
  inherited;
  cbRua.Text := '';
  cbBairro.Text := '';
  cbCidade.Text := '';
  cbimovel.Text := '';
  cbTipo.Text := '';
  valorDe.Text := '';
  valorAte.Text := '';
  q1.Text := '0';
  q2.Text := '0';
  s1.Text := '0';
  s2.Text := '0';
  c1.Text := '0';
  c2.Text := '0';
  g1.Text := '0';
  g2.Text := '0';
  cds.Close;
end;

procedure TfPesquisa_Locacao.BitBtn6Click(Sender: TObject);
begin
  inherited;
  Close;
end;

procedure TfPesquisa_Locacao.BitBtn7Click(Sender: TObject);
begin
  inherited;
  if cds.State in [dsBrowse] then
  begin
    fFoto := TfFoto.Create(Application);
    fFoto.codigofoto := '';
    try
      fFoto.codigofoto := IntToStr(cdsCOD_IMALU.AsInteger);
      fFoto.ShowModal;
    finally
      fFoto.Free;
    end;
  end;
end;

procedure TfPesquisa_Locacao.FormCreate(Sender: TObject);
var
  ScreenHeight: LongInt;
  ScreenWidth: LongInt;
begin
  // tamanho do form
{  ScreenWidth := 800;
  ScreenHeight := 600;
  if (screen.width <> ScreenWidth) then
  begin
    height := longint(height) * longint(screen.height) DIV ScreenHeight;
    width := longint(width) * longint(screen.width) DIV ScreenWidth;
    scaleBy(screen.width, ScreenWidth);
  end;     }
end;

procedure TfPesquisa_Locacao.FormShow(Sender: TObject);
begin
//  inherited;
  sCtrlResize.CtrlResize(TForm(fPesquisa_Locacao));
  if (dm.cds_cep.Active) then
      dm.cds_cep.Close;
  dm.cds_cep.Params[0].Clear;
  dm.cds_cep.Params[1].AsInteger := 9999999;
  dm.cds_cep.Open;
  dm.cds_cep.First;
  while not dm.cds_cep.Eof do
  begin
     cbRua.Items.Add(dm.cds_cepRUA.AsString);
     dm.cds_cep.Next;
  end;
  dm.cds_cep.Close;

  if (not dm.cds_bairro.Active) then
      dm.cds_bairro.Open;
  dm.cds_bairro.First;
  while not dm.cds_bairro.Eof do
  begin
     cbBairro.Items.Add(dm.cds_bairroBAIRRO.AsString);
     dm.cds_bairro.Next;
  end;
  dm.cds_bairro.Close;

  if (not dm.cds_cidade.Active) then
      dm.cds_cidade.Open;
  dm.cds_cidade.First;
  while not dm.cds_cidade.Eof do
  begin
     cbCidade.Items.Add(dm.cds_cidadeCIDADE.AsString);
     dm.cds_cidade.Next;
  end;
  dm.cds_cidade.Close;

  if (dm.cds_proprietario.Active) then
      dm.cds_proprietario.Close;
  dm.cds_proprietario.Params[0].Clear;
  dm.cds_proprietario.Params[1].AsInteger := 9999999;
  dm.cds_proprietario.Open;
  dm.cds_proprietario.First;
  while not dm.cds_proprietario.Eof do
  begin
     cbProprietario.Items.Add(dm.cds_proprietarioNOME_PROPRIETARIO.AsString);
     dm.cds_proprietario.Next;
  end;
  dm.cds_proprietario.Close;



end;

procedure TfPesquisa_Locacao.DBGrid1TitleClick(Column: TColumn);
begin
  inherited;
 cds.IndexFieldNames:=Column.FieldName;
end;

procedure TfPesquisa_Locacao.DBGrid1KeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  inherited;
   // mostro a primeira foto
   if dm.cds_foto.Active then
      dm.cds_foto.Close;
   dm.cds_foto.Params[0].Clear;
   dm.cds_foto.Params[0].AsString := IntTostr(cdsCOD_IMALU.AsInteger) + '-L';
   dm.cds_foto.Open;
   if not dm.cds_foto.IsEmpty then
     Image1.Picture.LoadFromFile(dm.cds_fotoNOME_FOTO.AsString)
   else
     Image1.Picture.LoadFromFile('');
   dm.cds_foto.Close;
end;

procedure TfPesquisa_Locacao.DBGrid1KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  inherited;
   // mostro a primeira foto
   if dm.cds_foto.Active then
      dm.cds_foto.Close;
   dm.cds_foto.Params[0].Clear;
   dm.cds_foto.Params[0].AsString := IntTostr(cdsCOD_IMALU.AsInteger) + '-L';
   dm.cds_foto.Open;
   if not dm.cds_foto.IsEmpty then
     Image1.Picture.LoadFromFile(dm.cds_fotoNOME_FOTO.AsString)
   else
     Image1.Picture.LoadFromFile('');
   dm.cds_foto.Close;
end;

procedure TfPesquisa_Locacao.DBGrid1DblClick(Sender: TObject);
begin
  inherited;
    fImovel_aluguel:=TfImovel_aluguel.create(Application); // cria form
    try
     if dm.cds_imo_aluguel.Active then
       DM.cds_imo_aluguel.Close;
     dm.cds_imo_aluguel.Params[0].Clear;
     DM.cds_imo_aluguel.Params[0].AsInteger:=cdsCOD_IMALU.AsInteger;
     DM.cds_imo_aluguel.Open;
     fImovel_aluguel.showModal; // exib form
    finally
     fImovel_aluguel.free; // libera o formulario da memoria
    end;
end;

procedure TfPesquisa_Locacao.ChkDBGridCellClick(Column: TColumn);
var
    Field: TField;
begin
    Field := Column.Field;
    if (Field <> nil) and (Field.Name = 'cdsVISITA')
        and Field.CanModify and not Column.ReadOnly then
      with Field.Dataset do begin
        if State = dsBrowse then
          Edit;
       if (cdsVISITA.AsString <> 'OK1') then
       begin
          if (cdsVISITA.AsString = '') then
          begin
            cdsVISITA.AsString := 'SIM';
            cds.Post;
          end
          else begin
            cdsVISITA.AsString := '';
            cdsVISITA.Clear;
          end;
       end
       else
          cdsVISITA.AsString := 'OK1';
      end;
end;

procedure TfPesquisa_Locacao.ChkDBGridColEnter(DBGrid: TDBGrid);
  var
    Field: TField;
  begin
    Field := DBGrid.SelectedField;
    if (Field <> nil) and (Field.Name = 'cdsVISITA') then
      DBGrid.Options := DBGrid.Options - [dgEditing];

end;

procedure TfPesquisa_Locacao.ChkDBGridColExit(DBGrid: TDBGrid);
  var
    Field: TField;
  begin
    Field := DBGrid.SelectedField;
    if (Field <> nil) and (Field.Name = 'cdsVISITA') then
      DBGrid.Options := DBGrid.Options + [dgEditing];

end;

procedure TfPesquisa_Locacao.ChkDBGridDrawColumnCell(DBGrid: TDBGrid;
  const Rect: TRect; DataCol: Integer; Column: TColumn;
  State: TGridDrawState);
var
  X, Y, Index: integer;
  Field: TField;
begin
  Field := Column.Field;
  if (Field <> nil) and (Field.Name = 'cdsVISITA') then
  begin
    DBGrid.Canvas.FillRect(Rect);
    // Determine the position of the graphic inside the cell
    case Column.Alignment of
    taRightJustify:
      X := Rect.Right - 2 - 16;
    taCenter:
      X := (Rect.Right - Rect.Left - 16) div 2 + Rect.Left;
    else // taLeftJustify:
      X := Rect.Left + 2;
    end;
    Y := (Rect.Bottom - Rect.Top - 16) div 2 + Rect.Top;
    // Determine the image to be used
    if cdsVISITA.AsString <> 'OK1' then
    begin
      if cdsVISITA.AsString = '' then
        Index := 1
      else
        Index := 0;
    end
    else
    begin
      Index := 2;
    end;
    // Draw the graphic
      ImageList1.Draw(DBGrid.Canvas, X, Y, Index);
  end
  else // Default drawing
    DBGrid.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;

procedure TfPesquisa_Locacao.ChkDBGridKeyPress(DBGrid: TDBGrid; var Key: Char);
var
    Field: TField;
begin
    Field := DBGrid.SelectedField;
    if (Field <> nil) and (Field.Name = 'cdsVISITA') then
      if (Key = ' ') and Field.CanModify and
          not DBGrid.Columns[DBGrid.SelectedIndex].ReadOnly then
        with Field.Dataset do begin
          if State = dsBrowse then
            Edit;
          if (cdsVISITA.AsString <> 'OK') then
          begin
            if (cdsVISITA.AsString = '') then
            begin
              cdsVISITA.AsString := 'Sim';
              cds.Post;
            end
            else begin
              cdsVISITA.AsString := '';
            end;
          end
          else
            cdsVISITA.AsString := 'OK';
        end;

end;

procedure TfPesquisa_Locacao.DBGrid1CellClick(Column: TColumn);
begin
  inherited;
  ChkDBGridCellClick(Column);
end;

procedure TfPesquisa_Locacao.DBGrid1ColEnter(Sender: TObject);
begin
  inherited;
  ChkDBGridColEnter(Sender as TDBGrid);
end;

procedure TfPesquisa_Locacao.DBGrid1ColExit(Sender: TObject);
begin
  inherited;
  ChkDBGridColEnter(Sender as TDBGrid);
end;

procedure TfPesquisa_Locacao.DBGrid1DrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn;
  State: TGridDrawState);
begin
  inherited;
  ChkDBGridDrawColumnCell(Sender as TDBGrid, Rect,
    DataCol, Column, State);
end;

procedure TfPesquisa_Locacao.dxButton1Click(Sender: TObject);
begin
  inherited;
   if (cds.Active) then
     cds.ApplyUpdates(0);
end;

procedure TfPesquisa_Locacao.dxButton2Click(Sender: TObject);
begin
  if cds.Active then
  begin
    fAlugar_Imovel := TfAlugar_Imovel.create(Application); // cria form
    try
      if dm.cds_imo_aluguel.Active then
       dm.cds_imo_aluguel.Close;
      dm.cds_imo_aluguel.Params[0].AsInteger := cdsCOD_IMALU.AsInteger;
      dm.cds_imo_aluguel.Open;
      dm.cds_imo_aluguel.Edit;
      fAlugar_Imovel.showModal; // exib form
    finally
      fAlugar_Imovel.free; // libera o formulario da memoria
    end;
  end;
  BitBtn1.Click;
end;

procedure TfPesquisa_Locacao.BitBtn5Click(Sender: TObject);
begin
  inherited;
 if (cdsVISITA.AsString = 'SIM') then
 begin
   fVisitar := TfVisitar.Create(Application);
   try
     fVisitar.ShowModal;
   finally
     fVisitar.Free;
   end;
 end;
 if (cdsVISITA.AsString = 'OK1') then
 begin
   fmostraVisita := TfmostraVisita.Create(Application);
   try
     fmostraVisita.codInteressado := cdsCOD_INTERESSADO.AsInteger;
     fmostraVisita.ShowModal;
   finally
     fmostraVisita.Free;
   end;
 end;
 BitBtn1.Click;
end;

end.
